﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbAdapter.sqldbAdapter;
using System.Data;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
  public  class DataAccessWork_ArticleUnit
    {
        /// <summary>
        /// 根据id获取劳资规格对象
        /// </summary>
        /// <returns></returns>
        public DataSet GetWork_ArticleUnitById(int Id)
        {
            sqldb db = new sqldb();
            string query = "select [ID],[AID],[Unit] ,[Remark] from Work_ArticleUnit where 1=1 and Id = " + Id;
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }


        /// <summary>
        /// 根据条件获取所有劳资规格。
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllWork_ArticleUnit(string condition)
        {
            sqldb db = new sqldb();
            string query = "select  A.[ID],A.[AID],A.[Unit] ,A.[Remark]  from Work_ArticleUnit A " + condition;
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 获取所有劳资规格(分页）
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllWork_ArticleUnit(string Condition, int pagesize, int startindex)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select  B.[ID],B.[AID],B.[Unit],B.[Remark]  from (select ROW_NUMBER() OVER(ORDER BY A.ID DESC) rownumber,"
                             + " A.[ID],A.[AID],A.[Unit],A.[Remark]  from Work_ArticleUnit A " + Condition + ") B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                return db.ExecuteDataset(conn, CommandType.Text, query);
            }
        }

        /// <summary>
        /// 保存或修改劳保物资规格
        /// </summary>
        /// <param name="dmpara"></param>
        /// <returns></returns>
        public int SaveWork_ArticleUnit(object[] ArticleUnitType)
        {
            sqldb db = new sqldb();
            if (Convert.ToInt32(ArticleUnitType[0]) > 0)
            {
                string update = "update Work_ArticleUnit set AID='" + ArticleUnitType[1] + "',Unit='" + ArticleUnitType[2] + "',Remark='" + ArticleUnitType[3] + "' where ID='" + ArticleUnitType[0] + "'";
                return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, update);
            }
            else
            {
                string insert = "insert  Work_ArticleUnit(AID,Unit,Remark) values('" + ArticleUnitType[1] + "','" + ArticleUnitType[2] + "','" + ArticleUnitType[3] + "')";
                return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, insert);
            }
            }


        /// <summary>
        /// 删除劳保物资规格
        /// </summary>
        /// <param name="did"></param>
        /// <returns></returns>
        public int DeleteWork_ArticleUnit(string id)
        {
            sqldb db = new sqldb();
            string query = "delete from Work_ArticleUnit where  id = " + id;
            return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 根据条件获取所有劳资规格数量
        /// </summary>
        /// <returns></returns>
        public int GetWork_ArticleUnitCount(string Condition)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Work_ArticleUnit " + Condition;
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

    }
}
